package com.gome.ocean.controller.filter;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import com.gome.framework.util.ObjectFactoryUtil;
import com.gome.ocean.common.constants.Constants;
import com.gome.ocean.dao.model.authority.User;
import com.gome.ocean.service.authority.IUserLoginLogoutService;
import com.google.common.collect.Maps;

/**
 * 
 * 对session 进行监听。用于进行 记录用户操作的信息
 * @author zhangguoqing
 * @implements HttpSessionListener
 */
public class AuthoritySession implements HttpSessionListener{
	
	private IUserLoginLogoutService userLoginLogoutService;
	
	/**
	 * 创建session信息
	 * */
	@Override
	public void sessionCreated(HttpSessionEvent se) {
		
	}
	
	/**
	 * session销毁或者session失效时候 触发
	 * */
	@Override
	public void sessionDestroyed(HttpSessionEvent se) {

		userLoginLogoutService = (IUserLoginLogoutService) ObjectFactoryUtil.getObject("@userLoginLogoutService");
		
		User user = (User)se.getSession().getAttribute(Constants.SESSION_LOGIN_USER);
		if (user!=null) {
			Long loginId = user.getLoginId();
			String email = user.getEmail();
			Map<String, Object> map = Maps.newHashMap();
			map.put("id", loginId);
			map.put("email", email);
			map.put("logoutTime", new Date());
			userLoginLogoutService.updateUserLoginLogoutInfoByMap(map);
		}
		
	}

	


}
